<template>
	<view class="mainpadding2" v-if="pageShow">
		<view class="" style="padding-bottom: 130rpx;">
			<view class="block flexleft margin_top" v-if="buy_type==1">
				<view :class="order_type==1?'nofocustext':'focustext'" @click="selectab(1)">到店自提</view>
				<view :class="order_type==2?'nofocustext':'focustext'" @click="selectab(2)">物流配送</view>
			</view>
			<!-- 物流配送 -->
			<view class="" v-if="order_type==2">
				<view class="ffffff radius mainpadding margin_top" v-if="addressData!=null" @click="tzshdz()">
					<view class="flexbetween">
						<view class="">
							<view class="flexleft">
								<view class="titletext zhonghei fonweight margin_right4">{{addressData.name}}</view>
								<view class="titletext zhonghei fonweight margin_right4">{{addressData.mobile}}</view>
							</view>				
							<view class="ershil xiaohui margin_top2">{{addressData.cityinfo}}{{addressData.address_detail}}</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="ffffff radius mainpadding ffffff margin_top" v-if="addressData==null" @click="tzshdz()">
					<view class="flexbetween">
						<view class="sanshier xiaohei fonweight">请选择收货地址</view>
						<u-icon name="arrow-right" color="#CCCCCC"></u-icon>
					</view>
				</view>
			</view>
			<view class="" v-for="item in orderData.lists" :key="item.shop_id">
				<view class="ffffff radius mainpadding margin_top" v-if="order_type==1">
					<view class="titletext zhonghei fonweight">{{item.shopinfo.name}}</view>
					<view class="ershil xiaohui margin_top2">{{item.shopinfo.address}}</view>
				</view>
				<view class="ffffff radius mainpadding margin_top">
					<view class="flexleft">
						<image src="@/static/images/tabbar/dianpus.png" class="margin_right1 dpimg" mode=""></image>
						<view class="ershib zhonghei bold">{{item.shopinfo.name}}</view>
					</view>
					<view class="flexbetween margin_top" v-for="ite in item.products" :key="ite.id">
						<image class="tupian" :src="ite.image_text" mode=""></image>
						<view class="" style="width: 78%;height: 124rpx;">
							<view class="ershib xiaohei yhxk">{{ite.title}}</view>
							<view class="smalltext xiaohui yhxk margin_top1">{{ite.sku2.difference.join("/")}}</view>
							<view class="flexbetween margin_top1">
								<view class="titletext fonweight xiaocheng" v-if="ly==1">¥{{ite.sku2.price}}</view>
								<view class="titletext fonweight xiaocheng" v-if="ly==2">¥{{ite.sku2.pt_price}}</view>
								<view class="titletext fonweight xiaocheng" v-if="ly==3">¥{{ite.sku2.ms_price}}</view>
								<view class="ershil xiaohui">×{{ite.number}}</view>
							</view>
						</view>
					</view>
					<view class="xiaohong flexbetween ershiba margin_top fonweight" v-if="order_type==2">
						<view class="">运费</view>
						<view class="">{{item.freight_price_bak}}</view>
					</view>
				</view>
				<view class="ffffff radius mainpadding margin_top">
					<view class="ershil xiaohei fonweight">备注</view>
					<textarea name="" id="" class="margin_top textarea" placeholder="请输入留言信息给卖家" v-model="item.remarks"></textarea>
				</view>
				<view class="ffffff radius mainpadding margin_top flexbetween" v-if="item.couponlist.length" @click="selectshop_id=item.shop_id;couponlist=item.couponlist;show=true">
					<view class="ershiba xiaohei fonweight">优惠券</view>
					<view class="flexright">
						<view class="ershiba xiaocheng margin_right1">{{item.coupon_id?"已选择":"有优惠可选"}}</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
			<view class="ffffff radius mainpadding margin_top flexbetween">
				<view class="ershiba xiaohei">发票</view>
				<u-radio-group v-model="is_openfp" placement="row" circle>
					<u-radio activeColor="#ff8505" :customStyle="{marginRight: '30rpx'}" label="否" :name="0"></u-radio>
					<u-radio activeColor="#ff8505" label="是" :name="1"></u-radio>
				</u-radio-group>
			</view>
			<view class="ffffff radius mainpadding margin_top">
				<view class="flexbetween">
					<view class="ershil xiaohei">应付金额</view>
					<view class="ershiba xiaohui fonweight">￥{{orderData.statis.allsub}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil xiaohei">联盟积分抵扣</view>
					<view class="ershiba xiaohui fonweight">￥{{orderData.statis.score_price}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil xiaohei">首单优惠</view>
					<view class="ershiba xiaohui fonweight">￥{{orderData.statis.sdyh_price}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil xiaohei">活动优惠</view>
					<view class="ershiba xiaohui fonweight">￥{{orderData.statis.activity_price}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil xiaohei">优惠券抵扣</view>
					<view class="ershiba xiaohui fonweight">￥{{orderData.statis.coupon_price}}</view>
				</view>
				<view class="flexbetween margin_top" v-if="order_type==2">
					<view class="ershil xiaohei">合计运费</view>
					<view class="ershiba xiaohui fonweight">￥{{orderData.statis.freight_price}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="ershil xiaohei">实际支付</view>
					<view class="ershiba xiaohong fonweight">￥{{orderData.statis.pay_price}}</view>
				</view>
			</view>
		</view>
		<view class="gudingdb ffffff mainpadding flexbetween">
			<view class="titletext xiaohei ">合计：
				<text class="bigtext fonweight xiaohong">{{orderData.statis.pay_price}}</text>
			</view>
			<view class="huabtn flexcenter" @click="goYuDingorder">立即支付</view>
		</view>
		<u-popup :show="show" @close="show=false" closeable :round="12">
			<view class="mainpadding" style="padding-top: 50rpx;">
				<scroll-view scroll-y="true" style="max-height: 500rpx;">
					<view class=" block1 margin_top mainpadding radius flexbetween" v-for="item in couponlist" :key="item.id">
						<view class="flexleft">
							<view class=" margin_right4">
								<view class="strongtext xiaocheng fonweight">￥
									<text class="sishi xiaocheng fonweight">{{item.price}}</text>
								</view>
								<view class="xiaohui strongtext margin_top2">满{{Number(item.mj_price)}}可用</view>
							</view>
							<view class="dqline margin_right"></view>
							<view class="">
								<view class="zhonghei sanshier fonweight">{{item.name}}</view>
								<view class="strongtext xiaohui margin_top2">有效期至{{item.end_time_text}}</view>
							</view>
						</view>
						<view class="wlhbtn flexcenter" @click="selectyhq(selectshop_id,item.id)">立即使用</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				pageShow:false,
				order_type: 1,//1到店自提，2物流配送
				buy_type:1,//1=实物商品,2=虚拟商品
				address_id:"",
				ly:1,//1=普通,2=拼团,3=秒杀
				wanlshop_group_id:"",
				lists:[],
				type:"detail",//detail=商品详情,cart=购物车
				addressData:null,
				orderData:null,
				selectshop_id:'',
				couponlist:[],
				show:false,
				is_openfp:0,
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.buy_type = data.buy_type
			this.ly = data.ly
			this.wanlshop_group_id = data.wanlshop_group_id
			this.type = data.type
			this.lists = data.lists
		},
		onShow() {
			uni.$off('addressId')
			uni.$on('addressId', data => {
				this.address_id = data
			})
			this.getdingdan()
		},
		methods: {
			goYuDingorder() {
				if (this.order_type == 2 && this.address_id=="") {
					console.log(this.address_id);
					httpRequest.toast("请选择收货地址")
					return false
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				let lists = []
				
				this.orderData.lists.forEach(item=>{
					let arr = []
					item.products.forEach(ite=>{
						arr.push({
							goods_id:ite.id,
							number:ite.number,
							sku_id:ite.sku_id
						})
					})
					lists.push({
						coupon_id:item.coupon_id,
						shop_id:item.shop_id,
						remarks:item.remarks,
						products:arr
					})
					
				})
				httpRequest.request('/api/order/addOrder', 'POST', {
					buy_type: this.buy_type,
					address_id: this.address_id,
					order_type: this.order_type,
					ly: this.ly,
					wanlshop_group_id: this.wanlshop_group_id,
					is_openfp:this.is_openfp,
					lists,
				}, false, false, false).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						if (this.type == "cart") {
							this.delcar()
						}
						let data = {
							id: res.data.order_pay_no,
							pay_price: res.data.pay_price,
							type: 2,
						}
						uni.redirectTo({
							url: '/pages_shouye/dingdanzf?msg=' + encodeURIComponent(JSON.stringify(data))
						})
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			delcar() {
				let arr = []
				this.lists.forEach(item => {
					item.products.forEach(ite=>{
						arr.push({
							goods_id:ite.goods_id,
							sku_id:ite.sku_id
						})
					})
				})
				httpRequest.request('/api/cart/delCart', 'POST', {
					data: arr,
				}).then(res => {})
			},
			selectyhq(pid,id){
				this.show = false
				this.lists.forEach(item=>{
					if(item.shop_id==pid){
						item.coupon_id=id
					}
				})
				this.getdingdan()
			},
			getdingdan() {
				let data = {
					buy_type: this.buy_type,
					address_id: this.address_id,
					order_type: this.order_type,
					ly: this.ly,
					wanlshop_group_id: this.wanlshop_group_id,
					lists: this.lists,
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				httpRequest.request("/api/order/getOrderGoodsList", 'POST', data, false, false, true).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						this.orderData = res.data.orderData
						this.addressData = res.data.addressData
						if (res.data.addressData && this.buy_type==1) {
							this.address_id = res.data.addressData.id
						}
						this.pageShow = true
					}else{
						httpRequest.toast(res.msg)
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 跳转收货地址
			tzshdz(){
				uni.navigateTo({
					url:'/pages_wode/shouhuodizhi?type=1'
				})
			},
			selectab(i) {
				if (i == this.order_type) {
					return false
				}
				this.order_type = i
				this.getdingdan()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.block1 {
		background: #FBF6F1;
		border-radius: 14rpx 14rpx 14rpx 14rpx;
	}
	.wlhbtn {
		width: 132rpx;
		height: 49rpx;
		background: rgba(255, 133, 5, 0.1);
		border-radius: 4rpx 4rpx 4rpx 4rpx;
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FF8505;
		border: 1rpx solid #FF8505;
	}
	.textarea{
		height: 100rpx;
	}
	.dpimg {
		width: 40rpx;
		height: 40rpx;
		min-width: 40rpx;
	}
	.huabtn {
		width: 250rpx;
		height: 76rpx;
		border-radius: 120rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		background-color: #FF9341;
	}

	.tupian {
		width: 124rpx;
		height: 124rpx;
		border-radius: 14rpx 14rpx 14rpx 14rpx;
	}

	.focustext {
		text-align: center;
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 800;
		width: 153rpx;
		height: 58rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #333333;
	}

	.nofocustext {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 153rpx;
		height: 58rpx;
		background: #FF7510;
		border-radius: 42rpx 42rpx 42rpx 42rpx;
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 800;
		color: #FFFFFF;
	}

	.block {
		background: #e2e2e2;
		width: 306rpx;
		height: 58rpx;
		border-radius: 42rpx 42rpx 42rpx 42rpx;
	}
</style>